const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");
// https://genshin.honeyhunterworld.com/?lang=CHS
const CLIPDATA_TYPE = {
    max:{
        fixedhp: 298.75,
        fixedAtt: 19.45,
        fixdefence: 23.15,
        reaction : 23.31,
        attRate: 5.83, 
        hpRate: 5.83, 
        criticalRate: 3.89,
        criticalStrike: 7.77,
        defRate: 7.29,
        charging: 6.48
    },
    mid:{
        fixedhp: 253.94,   // 209.13  239   268.88   298.75
        fixdefence: 19.675, // 16.2   18.52  20.83  23.15
        fixedAtt: 16.535,   //  13.62   15.56   17.51  19.45
        reaction : 19.815,  // 16.32   18.65  20.98  23.31
        attRate: 4.955,  // 4.08  4.66  5.25  5.83
        hpRate: 4.955,   // 4.08  4.66  5.25  5.83
        criticalRate: 3.305,   //  2.72  3.11   3.5   3.89
        criticalStrike: 6.605,  //  5.44  6.22  6.99  7.77
        defRate: 6.195,   //  5.1  5.83  6.56   7.29
        charging: 5.505  //  4.53   5.18   5.83   6.48
    }
}

const CLIPDATA = CLIPDATA_TYPE.mid

const CONFIG = {
    chance: 4,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        fixedHp: 4780,
        fixedAtt:311,
        attRate: 46.6,
        dmgRate: 46.6,
        criticalStrike: 62.2,
    },
    clips_initial: [
        ["fixedAtt", "attRate", "criticalRate", "criticalStrike"],
        ["fixedAtt", "attRate", "criticalRate", "criticalStrike"],
        ["fixedAtt", "fixdefence", "criticalRate", "criticalStrike"],
        ["fixedAtt", "attRate", "criticalRate", "criticalStrike"],
        ["fixedAtt", "attRate", "fixdefence", "criticalRate"]
    ],
    reactionK : 2,
    reactionDmgAdd: 0,
    buffs: [  
        { dmgRate: 15, attRate: 15},  // normal
        // { fixedAtt: 1203,attRate: 20}, // 班尼
        // { dmgRate:30}, // 夜兰
        // { jk: 20, attRate:20}, // 钟离
        // { dmgRate:15, jk: 20},  // 双岩
    ]
}

let charaters = {
    units : {
        name: "玛薇卡", 
        dmgZhi: 0,
        jf: 0,
        charging: 100,
        reaction: 0,
        defRate: 0,
        defence: 791.65, 
        weapontype: "sword2",
        attRate: 0,
        hp:12552,
        base: 358.77,    
        criticalRate: 5 ,    
        criticalStrike: 50 + 38.4,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}

let tmp = {}
CONFIG.clips_initial.forEach(item => {
    item.forEach(item2 => {
        if(tmp[item2]) {
            tmp[item2] += 1
        } else {
            tmp[item2] = 1
        }
    })
});

/*
    a: lv10: 158.21% 72.11%*2 65.67%*3 229.68%     z:383.18%

    e: 进入夜魂加持状态 点按召唤物后台跟随攻击 长按武装攻击 80点夜魂值上限
        lv10: 后台 230.4% 间隔2s
        lv10: 摩托车普攻 113.2 116.89 138.35 137.79 179.89   摩托车创人159.8  重击195.5  重击终结j272  下落攻击316.2 
       
    q: 战意到达50%可以开q  队伍有人消耗夜魂值增加战意 队伍有人普攻每次增加1.5战意    战意上限200
        释放后获得10夜魂值 进入夜魂加持状态 释放一下'坠日斩' 进入'死生之炉'状态7s
        死生之炉效果: 不消耗夜魂值 提高抗打断 坠日斩和e普攻重击增伤
        lv10 坠日斩: 800.64%    坠日斩倍率增加:战意x2.88%    e普攻倍率增加:战意x0.51%   e重击倍率增加:战意x1.02%


    天赋: 队伍有人触发夜魂迸发 + 30%攻击
        放q后根据当时战意 增加场上角色增伤 0.2%x战意 最高40  持续20s  并且20s内增伤会逐步降低到0

    命座:
    1. 夜魂值上限提升为120点，且战意的获取效率提升25%。玛薇卡的攻击力提升40%，持续8秒。
    2. 处于夜魂加持状态下时，玛薇卡的基础攻击力提升200点  e后台召唤物降低附近敌人防御20 普攻重击q倍率分别增加60%/90%/120%。
    3. q + 3
    4. 增伤天赋额外增加10增伤 并且不会降低了  也就是当前场上角色50增伤
    5. e + 3
    6. e后台攻击时 召唤一个摩托车创敌人 200%倍率   开摩托车时降低敌人20防御，每3秒造成500%倍率火伤
*/

let cons = 0 // 
let chooseIndex = 1; // 
let cid = "units"
let once = true
let count_kit = 0;
let combination = getCombination(Array(5*CONFIG.chance + 1).fill().map((_,i)=>i), CONFIG.chance, Object.entries(tmp))
let clips_initial_data = calc_clips_initial()
let fs = require("fs")
calc(combination);
function calc(clipSecondData) {
    let charactersDmg = [];
    clipSecondData = clipSecondData.map(clipSecondDataItem => {
        let clipSecond = {}
        clipSecond.des = clipSecondDataItem;
        clipSecond.data = {} 
        for (let secondKey in clipSecond.des) {
            let count =  clipSecond.des[secondKey]
            clipSecond.data[secondKey] = count * CLIPDATA[secondKey]
        }
        return clipSecond
    })
    
    Object.values(WEAPONS).forEach(weapon => {
        if(charaters[cid].weapontype && weapon.weapontype != charaters[cid].weapontype && weapon.weapontype!="all") {return}
        charactersDmg.push(getDMG([charaters[cid],weapon,clips_initial_data,{...CONFIG.clips_main}], clipSecondData, (data,second) => {
            count_kit++;
            data = objValueAdd([data, ...CONFIG.buffs]);
            
            if(weapon.skill) { weapon.skill(data); }

            let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:data.reactionDmgAdd || 0 }

            let eRate = [
                [1.08, 0.8784, 2.9983],
                [1.275, 1.037, 3.5397],
            ]
            if (cons >= 3) {
                eRate = eRate[0]
            } else {
                eRate = eRate[1]
            }
            // 
            let ezyDmg = getDmg({
                data: {
                    ...data,
                    dmgRate: data.dmgRate + (data.azDmg || 0)
                },
                rate: eRate[1]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, data.jf);
            
            if (once && Number.isNaN(ezyDmg)) {
                once = false
            }

            let ehgDmg = getDmg({
                data: {
                    ...data,
                    dmgRate: data.dmgRate + (data.azDmg || 0) + 65
                },
                rate: eRate[2]
            }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, data.jf);
            


            let qRate = [6.5146, 8.1432]   
            // let q1Dmg = getDmg({
            //     data: {
            //         ...data,
            //         dmgZhi:(data.dmgZhi||0) + data.defence*qRate[1],
            //         dmgRate: data.dmgRate,
            //     },
            //     rate: qRate[0]
            // }).qw * reduceResistanceK(10 - (data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, data.jf);
            

            let point = (ezyDmg * 3 + ehgDmg * 3) * 4
            
            let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
            return {point, data, second, attack, dmg:{ezyDmg,ehgDmg}}
        }))
    })
    
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point);
    });
    let sortf = JSON.parse(fs.readFileSync("./sort.js").toString()) 
    let choose = charactersDmg[chooseIndex];
    console.log(choose);
    // fs.writeFileSync("./ret.js", JSON.stringify(choose))

    let name = choose.data.name
    let keys = "_"+choose.data.weaponname+"_6命无拐2+2毕业"
    let sortfData = {key: name+keys, point:choose.point, dmg:JSON.parse(JSON.stringify(choose.dmg))}
    if(sortfData.point && sortfData.point > 0) {
        let org = sortf.findIndex(item3 => item3.key == sortfData.key)
        if(org >= 0){
            sortf[org] = sortfData
        } else {
            sortf.push(sortfData)
        }
        sortf.sort((b,a) => a.point-b.point)
        // fs.writeFileSync("./sort.js", JSON.stringify(sortf))
    }
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count_kit);
}

function getDMG(dataarr, clipSecond, getRet){
    clipSecond = clipSecond.map(second => {
        let panel = objValueAdd([...dataarr, second.data]);
        return  getRet(panel, second)
    })
    clipSecond.sort((b,a)=> a.point-b.point)
    return clipSecond[0]
}

function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
function getCombination (arr, chance, entrys) {
    let num = 5
    var r = [];
    (function f(t, a, n) {
        if (n === 0) {
            if(t.reduce((a1,b1) => a1+b1) !== chance * 5){return}
            let ret = {}
            for (let i in t) {
                if(t[i] > entrys[i][1]*chance){return}
                ret[entrys[i][0]] = t[i]
            }
            r.push(ret)
            return ;
        }
        for (var i = 0, l = a.length; i < l; i++) {
            f(t.concat(a[i]), a, n - 1);
        }
    })([], arr, num);
    return r;
}